package org.thread.test06.ts.homework.demo01;

/**
 * 女孩线程 - 吃水果
 * @author CSY
 * @date 2025 - 03 - 13
 */
public class GirlThread extends Thread{
    private Basket basket;

    public GirlThread(Basket basket) {
        this.basket = basket;
    }

    @Override
    public void run() {
        synchronized(BoyThread.LOCK){
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if(basket.getCount() == 0){
                    BoyThread.LOCK.notify();
                    System.out.println("小女孩吃完了，进入等待状态");
                    try {
                        BoyThread.LOCK.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else{
                    basket.setCount(basket.getCount() - 1);
                    System.out.println("小女孩吃了一个苹果");
                }

            }
        }
    }
}
